﻿using UnityEditor;
using System.IO;
using System.Collections.Generic;
using System;
using UnityEngine;

/// <summary>
/// 创建AB文件的列表xml
/// </summary>
public class CreateABMD5List
{
    public static void Execute(BuildTarget target)
    {
        Execute(target);
        AssetDatabase.Refresh();
    }

    public static void Execute(string path)
    {
        VersionInfo versionInfo = new VersionInfo();
        versionInfo.files = new List<VersionFile>();
        DirectoryInfo folder = new DirectoryInfo(path);

        foreach (FileInfo file in folder.GetFiles())
        {
            if (file.Name.Contains(".DS_Store") || file.Name.Contains(".meta") || file.Name.Contains(".txt"))
            {
                continue;
            }
            file.MoveTo(file.FullName.ToLower() + ".t");

            VersionFile ui = new VersionFile();
            ui.name = Path.GetFileName(file.FullName);
            ui.md5 = Util.GetFileMD5(file.FullName);
            versionInfo.files.Add(ui);
        }
        // 删除前一版的old数据
        if (File.Exists(path + "/VersionMD5-old.txt"))
        {
            File.Delete(path + "/VersionMD5-old.txt");
        }

        // 如果之前的版本存在，则将其名字改为VersionMD5-old.txt
        if (File.Exists(path + "/VersionMD5.txt"))
        {
            File.Move(path + "/VersionMD5.txt", path + "/VersionMD5-old.txt");
        }

        // 读取旧版本的MD5
        VersionInfo oldVersionInfo = ReadMD5File(path + "/VersionMD5-old.txt");

        if (File.Exists(path + "/VersionMD5-old.txt"))
        {
            File.Delete(path + "/VersionMD5-old.txt");
        }

        //oldVersionInfo中有，而versionInfo中没有的信息，手动添加到versionInfo
        foreach (VersionFile file in oldVersionInfo.files)
        {
            if (!versionInfo.files.Contains(file))
            {
                versionInfo.files.Add(file);
            }
        }
        string json = EditorJsonUtility.ToJson(versionInfo);
        StreamWriter sw = new StreamWriter(path + "/VersionMD5.txt");
        sw.Write(json);
        sw.Close();
    }

    /// <summary>
    /// 读取文件md5列表
    /// </summary>
    /// <param name="fileName"></param>
    /// <returns></returns>
    public static VersionInfo ReadMD5File(string fileName)
    {
        VersionInfo versionInfo = new VersionInfo();
        versionInfo.files = new List<VersionFile>();
        versionInfo.version = 0;
        try
        {
            // 如果文件不存在，则直接返回
            if (File.Exists(fileName) == false)
            {
                return versionInfo;
            }
            StreamReader sr = new StreamReader(fileName);
            string fileListString = sr.ReadToEnd();
            EditorJsonUtility.FromJsonOverwrite(fileListString, versionInfo);
        }
        catch (Exception e)
        {
            Debug.LogError(e.ToString());
        }
        return versionInfo;
    }
}

[Serializable]
/// <summary>
/// 更新的版本信息
/// </summary>
public class VersionInfo
{
    /// <summary>
    /// 版本号
    /// </summary>
    public int version;
    /// <summary>
    /// 更新的内容
    /// </summary>
    public List<VersionFile> files = new List<VersionFile>();
}

[Serializable]
/// <summary>
/// 更新内容信息
/// </summary>
public class VersionFile
{
    /// <summary>
    /// 名称
    /// </summary>
    public string name;
    /// <summary>
    /// md5
    /// </summary>
    public string md5;
}